<?php

namespace App\V3\V3_2_6\Services;

use App\V3\V3_2_6\Models\MaterielContentsModel;
use App\V3\V3_2_6\Models\MaterielsModel;


class MaterielsService
{
    static public function materielExport($data, $userId)
    {
        if(empty($data['keyword'])) {
            $result = MaterielsModel::search($data, $userId);
        }
        else {
            $result = self::withKeyword($data, $userId);
        }

        foreach($result as &$value) {
            foreach($value['materiel_content'] as &$val) {
                $serialNumber = empty($val['serial_no'])?[]:array_filter(explode(',', $val['serial_no']), function($v) { return $v !== ''; });

                if(empty($serialNumber)) {
                    $value['serial_line'] = 1;
                }
                else {
                    $value['serial_line'] = count($serialNumber);
                }

//                $val['serial_no'] = $serialNumber;
            }
        }

        return $result;
    }

    static protected function withKeyword($data, $userId)
    {
        $materielIdInfo = [];
        switch($data['search_type']) {
            case 0:
                $condition[] = ['b.type_name', 'like', '%'.$data['keyword'].'%'];
                $materielIdInfo = MaterielContentsModel::getMaterielId($condition, $userId);
                break;
            //商品名
            case 1:
                $condition[] = ['b.materiel_name', 'like', '%'.$data['keyword'].'%'];
                $materielIdInfo = MaterielContentsModel::getMaterielId($condition, $userId);
                break;
            //商品编号
            case 2:
                $condition[] = ['b.materiel_no', 'like', '%'.$data['keyword'].'%'];
                $materielIdInfo = MaterielContentsModel::getMaterielId($condition, $userId);
                break;
            //内部单号
            case 3:
                $condition[] = ['purchase_no', 'like', '%'.$data['keyword'].'%'];
                $materielIdInfo = MaterielsModel::getMaterielId($condition, $userId);
                break;
            //外部单号
            case 4:
                $condition[] = ['out_order_no', 'like', '%'.$data['keyword'].'%'];
                $materielIdInfo = MaterielsModel::getMaterielId($condition, $userId);
                break;
            //快递单号
            case 5:
                $condition[] = ['logistics_order_no', 'like', '%'.$data['keyword'].'%'];
                $materielIdInfo = MaterielsModel::getMaterielId($condition, $userId);
                break;
            //供应商
            case 6:
                $condition[] = ['supplier_name', 'like', '%'.$data['keyword'].'%'];
                $materielIdInfo = MaterielsModel::getMaterielId($condition, $userId);
                break;
            //司机名称
//            case 7:
//                $condition[] = ['logistics_order_no', 'like', '%'.$data['keyword'].'%'];
//                $materielIdInfo = MaterielsModel::getMaterielId($condition, $userId);
//                break;
            //发货人
            case 8:
                $condition[] = ['deliver_person', 'like', '%'.$data['keyword'].'%'];
                $materielIdInfo = MaterielsModel::getMaterielId($condition, $userId);
                break;
        }

        $materielIdArr = [];
        foreach($materielIdInfo as $value) {
            $materielIdArr[] = $value['materiel_id'];
        }

        return MaterielsModel::search($data, $userId, $materielIdArr);
    }
}

